package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 权限控制器
 * @author xgm
 * @date 2020/11/21 10:36
 */
@RestController
@Slf4j
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    //分页查询
    @RequestMapping("/findPage")
    //@PreAuthorize("hasAuthority('Administrator')")
    public Result findPage(QueryPageBean queryPageBean){
        log.info("[权限-分页查询]data:{}",queryPageBean);
        //rpc查询数据
        PageResult pageResult = permissionService.pageQuery(queryPageBean);
        //List<Permission> list = permissionService.findAll();
        //封装返回数据
        return new Result(true,"查询成功",pageResult);
    }

    /**
     * 新增
     * 1、rpc调用完成新增业务
     * 2、返回结果
     * @param permission
     * @return
     */
    @RequestMapping("/add")
    //@PreAuthorize("hasAuthority('Permission_ADD')")
    public Result add(@RequestBody Permission permission){
        log.info("[权限-新增]data:{}",permission);
        permissionService.add(permission);
        return new Result(true,"新增权限成功");
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    //@PreAuthorize("hasAuthority('PERMISSION_DELETE')")
    public Result delete(Integer id){
        log.info("[权限-根据id删除]id：{}",id);
        //RPC调用处理业务
        permissionService.delete(id);
        return new Result(true,"删除成功");
    }


    /**
     * 编辑  提供id查找到数据并回显
     *
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.info("[编辑前先回显数据],id,{},id");
        Permission permission = permissionService.findById(id);
        log.info("data:{}",permission);
        return new Result(true,"回显成功",permission);
    }



    /**
     * 编辑
     * @param permission
     * @return
     */
    @RequestMapping("/edit")
    //@PreAuthorize("hasAuthority('PERMISSION_EDIT')")
    public Result edit(@RequestBody Permission permission){
        log.info("[权限-编辑]permission,{}",permission);
        //rpc
        permissionService.edit(permission);
        return new Result(true,"更新成功");
    }

}
